package com.hangwei.paperless.presenter;

import android.support.v4.widget.SwipeRefreshLayout;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.hangwei.paperless.model.bean.ProcessSheet;
import com.hangwei.paperless.net.NetManager;
import com.twelve.all.net.ResponseInfo;
import com.twelve.all.utils.SPKey;
import com.twelve.all.utils.SPUtil;
import com.twelve.all.utils.ToastUtil;

import java.util.List;

import rx.Observable;

public class ProcessListPresenter extends RecyclerPresenter<ProcessSheet>{

    private final String mUserId;
    private String woNo;
    private String pageOrderName;
    private String pageOrderBy;

    public void setWoNo(String woNo) {
        this.woNo = woNo;
    }

    public void setPageOrderName(String pageOrderName) {
        this.pageOrderName = pageOrderName;
    }

    public void setPageOrderBy(String pageOrderBy) {
        this.pageOrderBy = pageOrderBy;
    }

    public ProcessListPresenter(SwipeRefreshLayout refreshLayout, BaseQuickAdapter<ProcessSheet, BaseViewHolder> adapter) {
        super(refreshLayout, adapter);
        mUserId = SPUtil.getString(SPKey.Account.USER_ID);
        PAGE_SIZE = 20;
    }

    @Override
    protected Observable<ResponseInfo<List<ProcessSheet>>> getObservable(int page) {
        return NetManager.getResponseInfoAPI().getBushingSheetList(mUserId,woNo,page,PAGE_SIZE,pageOrderName,pageOrderBy);
    }

    @Override
    protected void setData(boolean isRefresh, List<ProcessSheet> data) {
        mNextRequestPage++;
        final int size = data == null ? 0 : data.size();
        if (isRefresh) {
            if (data != null)
                for (int i = 0; i < data.size(); i++) {
                    if (i % 2 == 0)
                        data.get(i).whiteBg = true;
                }
            mAdapter.setNewData(data);
        } else {
            if (mNextRequestPage == 1) {
                if (data != null)
                    for (int i = 0; i < data.size(); i++) {
                        if (i % 2 == 0)
                            data.get(i).whiteBg = true;
                    }
                mAdapter.setNewData(data);
            } else if (size > 0) {
                mAdapter.addData(data);
                for (int i = 0; i < mAdapter.getData().size(); i++) {
                    if (i % 2 == 0)
                        mAdapter.getData().get(i).whiteBg = true;
                }
                mAdapter.notifyDataSetChanged();
            }
        }
        if (size < PAGE_SIZE) {
            //第一页如果不够一页就不显示没有更多数据布局(    不显示且不足一屏就自动加载，page还不对 )
//            mAdapter.loadMoreEnd(!isRefresh);
            mAdapter.loadMoreEnd(false);
            if (mNextRequestPage == 1) {//不需要的提醒
                if (isRefresh) {
                    ToastUtil.showToast("数据更新成功");
                } else
                    ToastUtil.showToast("没有更多数据了");
            }
        } else {
            mAdapter.loadMoreComplete();
        }
    }
}
